package com.example.shiyan6;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

//import org.json.JSONObject;
import com.google.gson.Gson;

import org.json.JSONArray;
import net.sf.json.JSONObject;





import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    final String appkey="2c913c05d92e73b271e5a4a3702539b4";
    //String timeindex="1418745237";
    final String urlString="http://v.juhe.cn/joke/randJoke.php?key="+appkey;

    List<Result> dataList;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
       //final ListView list=findViewById(R.id.list);




        Button button_get =findViewById(R.id.get);
        button_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(){
                    public void run() {

                        try {
                            URL url=new URL(urlString);
                            HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
                            urlConnection.setConnectTimeout(5000);
                            urlConnection.setReadTimeout(5000);
                            urlConnection.setRequestMethod("GET");
                            urlConnection.connect();
                            int code=urlConnection.getResponseCode();
                            // Log.i("44444444444444", "code="+code);
                            if (code==200) {
                                InputStream inputStream=urlConnection.getInputStream();
                                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                                String line;
                                StringBuffer buffer=new StringBuffer();
                                while ((line=bufferedReader.readLine())!=null) {
                                    buffer.append(line);

                                }
                                String str=buffer.toString();
                                Message message=new Message();
                                message.what=0;
                                message.obj=str;
                                //发送Handler消息
                                handler.sendMessage(message);

                            }

                        } catch (MalformedURLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }.start();
               // myadapter=new Myadapter(dataList,MainActivity.this);
                //list.setAdapter(myadapter);
                Toast.makeText(MainActivity.this,"test",Toast.LENGTH_LONG).show();


            }
        });


    }



    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
//清空集合
//                    news.clear();
//刷新适配器
                   // myadapter.notifyDataSetChanged();
                    String text=(String) msg.obj;
                    String joketext;
                    //转换json为object
                    Gson gson=new Gson();//新建gson对象
                    jokes joke=gson.fromJson(text,jokes.class);//jokes是自定义的实体类
                       dataList=joke.getResult();

                    joketext=dataList.get(0).getContent();
                    TextView textView=findViewById(R.id.text);
                    textView.setText(joketext);
                    break;

                default:
                    break;
            }
        }
    };



}
